/**
 * 2010(c) Copyright Oceansoft Information System Co.,LTD. All rights reserved.
 * <p>
 * Compile: JDK 1.6+
 * <p>
 * 版权所有(C)：江苏欧索软件有限公司
 * <p>
 * 公司名称：江苏欧索软件有限公司
 * <p>
 * 公司地址：中国苏州科技城青山路1号
 * <p>
 * 网址: http://www.oceansoft.com.cn
 * <p>
 * 作者: 090922(陈伟)
 * <p>
 * 文件名: com.oceansoft.emobile.biz.api.controller.ApiProfileController.java 
 * <p>
 * 类产生时间: 2014/4/19 23:12
 * <p>
 * 负责人: 090922(陈伟)
 * <p>
 * Email:javacspring@gmail.com
 * <p>
 * 所在组 : 掌上公安应用平台
 * <p>
 * 所在部门: 开发部--手持技术部
 * <p>
 * <p>
 */
package cn.com.oceansoft.business.sys.controller;

import cn.com.oceansoft.business.base.controller.BaseController;
import cn.com.oceansoft.business.base.entity.Result;
import cn.com.oceansoft.business.danger.entity.CollecterInfo;
import cn.com.oceansoft.business.danger.service.ICollecterInfoService;
import cn.com.oceansoft.business.sys.entity.SysUser;
import cn.com.oceansoft.business.sys.service.ISysResourceService;
import cn.com.oceansoft.business.sys.service.ISysUserService;
import cn.com.oceansoft.nos.common.util.MD5Util;
import cn.com.oceansoft.nos.common.util.SessionUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Set;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * 用户账号管理控制器
 *
 * @author: chenw
 * @time: 2014/4/19 23:12
 */
@Controller
@RequestMapping("profile")
public class ProfileController extends BaseController {

    @Resource
    private ISysUserService sysUserService;

    @Resource
    private ISysResourceService resourceService;

    @Resource
    private ICollecterInfoService collecterInfoService;




    @RequestMapping("login")
    public String gotoLogin(HttpServletRequest request,Model model) {

        try {
            String guid = "";

            if(null != getSession()){
                return gotoIndex(request,model);
            }


            if(StringUtils.hasText(guid)){
                //获取用户信息
                CollecterInfo collecter=new CollecterInfo();
                collecter.setLoginname(guid);
                CollecterInfo collecterInfo=collecterInfoService.getUser(collecter);
                request.getSession().setAttribute("aqyhuser",collecterInfo);
                log(String.format("用户%s登录成功", collecter.getLoginname()));
                //跳转至首页
                return gotoIndex(request,model);
            }
        }catch (RuntimeException e){
            return "admin/profile-login";
        }
       // System.out.println("========"+ MD5Util.getMD5String("123455"));

        return "admin/profile-login";
    }

    @RequestMapping("re-login")
    public String gotoReLogin() {
        return "admin/ajax-login";
    }

    @RequestMapping("index")
    public String gotoIndex(HttpServletRequest request,Model model) {
        //加载当前用户菜单
       /* String flag = request.getParameter("flag");
        if(StringUtils.hasText(flag)){
            model.addAttribute("flag",flag);
        }*/
        CollecterInfo cInfo =(CollecterInfo)request.getSession().getAttribute("aqyhuser");
        if("0".equals(cInfo.getRoletype())){
            model.addAttribute("roletype","0");//区域
        }
        if("1".equals(cInfo.getRoletype())){
            model.addAttribute("roletype","1");//街道
        }
        if("2".equals(cInfo.getRoletype())){
            model.addAttribute("roletype","2");//社区
        }
        return "admin/console-index";
    }

    @RequestMapping(value = "setting", method = GET)
    public String gotoSetting() {
        return "admin/profile-setting";
    }

    @ResponseBody
    @RequestMapping(value = "setting", method = POST)
    public Result updatePassword(CollecterInfo collecterInfo) {
        CollecterInfo collecter=new CollecterInfo();
        collecter=getMemberSession();
        collecter.setPassword(collecterInfo.getPassword());
        collecter.setOldpassword(collecterInfo.getOldpassword());
       /* SysUser sess = getSession();
        sess.setPassword(user.getPassword());
        sess.setOldPwd(user.getOldPwd());
        log(String.format("用户%s修改个人信息", sess.getLoginId()));
        return sysUserService.updateUserAccountInfo(sess);*/
        return collecterInfoService.updatePassword(collecter);
    }


    @ResponseBody
    @RequestMapping(value = "auth", method = POST)
    public Result loginAuth(CollecterInfo collecterInfo,HttpServletRequest request) {
        if (StringUtils.hasText(collecterInfo.getLoginname()) && StringUtils.hasText(collecterInfo.getPassword())) {
            Result rt =collecterInfoService.loginAuthen(collecterInfo);
            if (rt.isSucc()) {
               request.getSession().setAttribute("aqyhuser",(CollecterInfo)rt.getData());
                log(String.format("用户%s登录成功", collecterInfo.getLoginname()));
            } else {
                log(String.format("用户%s尝试登录系统失败，用户名或密码错误", collecterInfo.getLoginname()));
            }
            return rt;
        } else {
            log(String.format("用户%s尝试登录系统失败，用户名或密码错误", collecterInfo.getLoginname()));
            return new Result(Result.FAILURE, "用户名或密码不能为空");
        }
    }

    @ResponseBody
    @RequestMapping(value = "logout", method = GET)
    public Result logout(HttpSession session) {
        log(String.format("用户%s退出系统", getSession()));
        SessionUtil.invalidate(session);
        return new Result();
    }
}
